<!-- 今日需联系客户 -->
<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CrmCustomerApi } from '#/api/crm/customer';

import { useRouter } from 'vue-router';

import { Button } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getCustomerPage } from '#/api/crm/customer';
import { useGridColumns } from '#/views/crm/customer/data';

import { CONTACT_STATUS, convertOptions, SCENE_TYPES } from '../data';

import { $t } from '#/locales'

const { push } = useRouter();

/** 打开客户详情 */
function handleDetail(row: CrmCustomerApi.Customer) {
  push({ name: 'CrmCustomerDetail', params: { id: row.id } });
}

function useGridFormSchema() {
  return [
    {
      fieldName: 'contactStatus',
      label: $t('common.status'),
      component: 'Select',
      componentProps() {
        return {
          allowClear: true,
          options: convertOptions(CONTACT_STATUS),
        }
      },
      defaultValue: CONTACT_STATUS[0]!.value,
    },
    {
      fieldName: 'sceneType',
      label: $t('views.crm.todo.sceneType'),
      component: 'Select',
      componentProps() {
        return {
          allowClear: true,
          options: convertOptions(SCENE_TYPES),
        }
      },
      defaultValue: SCENE_TYPES[0]!.value,
    },
  ]
}

const [Grid] = useVbenVxeGrid({
  formOptions: {
    schema: useGridFormSchema(),
  },
  gridOptions: {
    columns: useGridColumns(),
    height: 'auto',
    keepSource: true,
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          return await getCustomerPage({
            pageNo: page.currentPage,
            pageSize: page.pageSize,
            pool: null, // 是否公海数据
            ...formValues,
          });
        },
      },
    },
    rowConfig: {
      keyField: 'id',
      isHover: true,
    },
    toolbarConfig: {
      refresh: true,
      search: true,
    },
  } as VxeTableGridOptions<CrmCustomerApi.Customer>,
});
</script>

<template>
  <Grid>
    <template #name="{ row }">
      <Button type="link" @click="handleDetail(row)">{{ row.name }}</Button>
    </template>
    <template #actions="{ row }">
      <Button type="link" @click="handleDetail(row)">
        {{ $t('common.detail') }}
      </Button>
    </template>
  </Grid>
</template>
